<?php
// -----------------------------------------------------------------------------
// include
// -----------------------------------------------------------------------------

include "../config/config.php";
include "../auth.php";
include "../DBManager.php";

// -----------------------------------------------------------------------------
// 数据库相关设置
// -----------------------------------------------------------------------------

$dbPath = "../" . $dbPath; // 不加这行语句的话会报错，提示找不到对应路径
$db = new DBManager("sqlite:" . $dbPath);

// -----------------------------------------------------------------------------
// 时区设置
// -----------------------------------------------------------------------------

// 设置时区
date_default_timezone_set($region);

$todayStr = date("Ymd");

// -----------------------------------------------------------------------------
// 函数定义
// -----------------------------------------------------------------------------

/**
 * 传入一个表示日期的字符串，返回当日的星期天数。
 * @param $dateString 一个表示日期的字符串，如 20240729
 * @return string 当日的星期天数，如“日”，“六“。
 * @throws Exception
 */
function getDayOfWeek($dateString) {
    $days = ["日", "一", "二", "三", "四", "五", "六"];
    $dt = new DateTime($dateString);
    $index = $dt->format("w");
    return $days[$index];
}

function generateLink($date)
{

    $dayOfWeek = getDayOfWeek($date);

    $tomorrowStr = date("Ymd", strtotime("tomorrow"));
    $theDayAfterTomorrowStr = date("Ymd", strtotime('tomorrow + 1day'));
    $threeDaysLaterStr = date("Ymd", strtotime('tomorrow + 2days'));

    $mark = "";
    switch ($date) {
        case $tomorrowStr:
            $mark = " (明天)";
            break;
        case $theDayAfterTomorrowStr:
            $mark = " (后天)";
            break;
        case $threeDaysLaterStr:
            $mark = " (大后天)";
            break;
    }

    echo <<<BBB
        <a href="../viewByDate.php?date={$date}" class="list-group-item list-group-item-action">{$date} 周{$dayOfWeek}{$mark}</a>
    BBB;

}

// -----------------------------------------------------------------------------

$futureDatesWithTasks = $db->getListOfFutureDateWithTask($todayStr);

if (count($futureDatesWithTasks) == 0) {

    echo <<<BBB
        <div class="mt-2 alert alert-warning">
            <p>暂时没有相关记录。</p>
            <p>如果你在未来某日的日程查看界面的「当日」中添加了日程，该日日期会显示在这里。</p>
            <p class="mb-0">这可以当成「<span class="text-info">备忘录</span>」来使用。</p>
        </div>
    BBB;

} else {

    // -------------------------------------------------------------------------

    $listOfFutureDatesInThisWeek = [];
    $listOfFutureDatesInThisMonth = [];
    $listOfFutureDatesInThisYear = [];
    $listOfFutureDatesAfterThisYear = [];
    $yearsOfFutureDateAfterThisYear = [];

    // 今天是哪一年
    $yearOfToday = date("Y", strtotime($todayStr));
    // 今天是哪一月
    $monthOfToday = date("m", strtotime($todayStr));
    // 今天是哪一周
    $weekOfToday = date("W", strtotime($todayStr));

    foreach ($futureDatesWithTasks as $futureDate) {

        // 这天是哪一年
        $yearOfFutureDate = date("Y", strtotime($futureDate));
        // 这天是哪一月
        $monthOfFutureDate = date("m", strtotime($futureDate));
        // 这天是哪一周
        $weekOfFutureDate = date("W", strtotime($futureDate));

        if ($yearOfFutureDate !== $yearOfToday) {
            // 该日不在今年范围内

            // 数组去重
            if (!in_array($yearOfFutureDate, $yearsOfFutureDateAfterThisYear)) {
                $yearsOfFutureDateAfterThisYear[] = $yearOfFutureDate;
            }

            $listOfFutureDatesAfterThisYear[] = $futureDate;

        } else {
            // 该日在今年范围内

            if ($weekOfFutureDate == $weekOfToday) {
                // 该日在本周范围内
                $listOfFutureDatesInThisWeek[] = $futureDate;
            } elseif ($monthOfFutureDate == $monthOfToday) {
                // 该日在本月范围内
                $listOfFutureDatesInThisMonth[] = $futureDate;
            } else {
                // 该日在今年范围内
                $listOfFutureDatesInThisYear[] = $futureDate;
            }

        }

    }

    // -------------------------------------------------------------------------
    // 打印结果
    // -------------------------------------------------------------------------

    $dayOfWeekOfToday = getdayofweek($todayStr);
    echo <<<BBB
        <div class="mt-0 mb-0 alert alert-warning">
            <p class="mb-0">今天是 <span class="text-info">$todayStr 周{$dayOfWeekOfToday}</span></p>
        </div>
    BBB;

    /* in this week */

    if (count($listOfFutureDatesInThisWeek) !== 0) {

        echo <<<BBB
            <div class="mt-3">
                <h3>本周内</h3>
                <div class="list-group">
        BBB;

        foreach ($listOfFutureDatesInThisWeek as $date) {
            generateLink($date);
        }

        echo <<<BBB
                </div>
            </div>
        BBB;

    }

    // -------------------------------------------------------------------------

    /* in this month */

    if (count($listOfFutureDatesInThisMonth) !== 0) {

        echo <<<BBB
            <div class="mt-3">
                <h3>本月内</h3>
                <div class="list-group">
        BBB;

        foreach ($listOfFutureDatesInThisMonth as $date) {
            generateLink($date);
        }

        echo <<<BBB
                </div>
            </div>
        BBB;

    }

    // -------------------------------------------------------------------------

    /* in this year */

    if (count($listOfFutureDatesInThisYear) !== 0) {

        echo <<<BBB
            <div class="mt-3">
                <h3>今年内</h3>
                <div class="list-group">
        BBB;

        foreach ($listOfFutureDatesInThisYear as $date) {
            generateLink($date);
        }

        echo <<<BBB
                </div>
            </div>
        BBB;

    }

    // -------------------------------------------------------------------------

    /* after this year */

    if (count($listOfFutureDatesAfterThisYear) !== 0) {

        foreach ($yearsOfFutureDateAfterThisYear as $year) {

            $title = "";
            if ($year == ($yearOfToday + 1)) {
                $title = "明年内";
            } else {
                $title = $year;
            }

            echo <<<BBB
                <div class="mt-3">
                    <h3>$title</h3>
                    <div class="list-group">
            BBB;

            foreach ($listOfFutureDatesAfterThisYear as $date) {

                $yearOfFutureDate = date("Y", strtotime($date));

                if ($year == $yearOfFutureDate) {
                    generateLink($date);
                }

            }

            echo <<<BBB
                    </div>
                </div>
            BBB;

        }

    }

    // -------------------------------------------------------------------------
}
